{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Ch `02`: Concept `08`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Using TensorBoard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "TensorBoard is a great way to visualize what's happening behind the code. \n",
    "\n",
    "In this example, we'll loop through some numbers to improve our guess of the average value. Then we can visualize the results on TensorBoard. \n",
    "\n",
    "Let's just set ourselves up with some data to work with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "raw_data = np.random.normal(10, 1, 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The moving average is defined as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "alpha = tf.constant(0.05)\n",
    "curr_value = tf.placeholder(tf.float32)\n",
    "prev_avg = tf.Variable(0.)\n",
    "\n",
    "update_avg = alpha * curr_value + (1 - alpha) * prev_avg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Here's what we care to visualize:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "avg_hist = tf.summary.scalar(\"running_average\", update_avg)\n",
    "value_hist = tf.summary.scalar(\"incoming_values\", curr_value)\n",
    "\n",
    "merged = tf.summary.merge_all()\n",
    "writer = tf.summary.FileWriter(\"./logs\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Time to compute the moving averages. We'll also run the `merged` op to track how the values change:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.7201648998 0.536008\n",
      "8.48030241652 0.933223\n",
      "8.30247090956 1.30169\n",
      "9.24097542488 1.69865\n",
      "10.2088633934 2.12416\n",
      "10.3614895822 2.53603\n",
      "9.37371628589 2.87791\n",
      "10.9862606878 3.28333\n",
      "10.9225657991 3.66529\n",
      "10.4472644201 4.00439\n",
      "9.99326579845 4.30383\n",
      "9.57330486057 4.56731\n",
      "9.45675635064 4.81178\n",
      "9.9941788904 5.0709\n",
      "9.09836853805 5.27227\n",
      "12.0245321032 5.60989\n",
      "10.2562304744 5.8422\n",
      "10.1191878763 6.05605\n",
      "8.70716447101 6.18861\n",
      "8.90712163427 6.32453\n",
      "10.5681769153 6.53672\n",
      "10.9797007819 6.75886\n",
      "10.6030210585 6.95107\n",
      "10.9491388896 7.15098\n",
      "11.6004465495 7.37345\n",
      "8.91282749896 7.45042\n",
      "10.763125555 7.61605\n",
      "9.41257928138 7.70588\n",
      "9.79326530865 7.81025\n",
      "9.44668651831 7.89207\n",
      "9.51611890893 7.97327\n",
      "10.4691714006 8.09807\n",
      "9.91965385005 8.18915\n",
      "11.2162023225 8.3405\n",
      "9.63270946431 8.40511\n",
      "9.0511922839 8.43742\n",
      "7.42832296956 8.38696\n",
      "9.6116479518 8.44819\n",
      "9.43684146689 8.49763\n",
      "10.7361498484 8.60955\n",
      "8.78547061442 8.61835\n",
      "10.4454290488 8.7097\n",
      "10.3123350408 8.78983\n",
      "12.0810408439 8.95439\n",
      "10.8530456005 9.04932\n",
      "9.49097084294 9.07141\n",
      "10.0076869787 9.11822\n",
      "10.2599465527 9.17531\n",
      "9.35027184761 9.18406\n",
      "10.3080742794 9.24026\n",
      "8.35716043518 9.1961\n",
      "10.7478332848 9.27369\n",
      "11.3755526586 9.37878\n",
      "9.96163924982 9.40793\n",
      "10.2307646228 9.44907\n",
      "9.40638656004 9.44693\n",
      "9.2572957088 9.43745\n",
      "10.4661914119 9.48889\n",
      "9.53670903581 9.49128\n",
      "12.8818178743 9.66081\n",
      "8.48437820968 9.60198\n",
      "9.91886957958 9.61783\n",
      "11.016601438 9.68777\n",
      "11.209783608 9.76387\n",
      "10.4102438984 9.79619\n",
      "11.421204522 9.87744\n",
      "8.37095962514 9.80211\n",
      "10.8108074925 9.85255\n",
      "11.1384575597 9.91685\n",
      "11.1040471909 9.97621\n",
      "11.400325371 10.0474\n",
      "9.31651788534 10.0109\n",
      "9.43111069234 9.98188\n",
      "10.6546148549 10.0155\n",
      "12.9366576045 10.1616\n",
      "8.51586473864 10.0793\n",
      "10.6277159529 10.1067\n",
      "10.8474604476 10.1437\n",
      "9.78387739003 10.1258\n",
      "9.85021562296 10.112\n",
      "10.2505734761 10.1189\n",
      "9.70002484695 10.098\n",
      "10.2094642982 10.1035\n",
      "7.88918640832 9.99282\n",
      "8.66084207803 9.92622\n",
      "10.6783114899 9.96382\n",
      "10.1540925096 9.97334\n",
      "10.3180377247 9.99057\n",
      "11.4462344283 10.0634\n",
      "9.81404206269 10.0509\n",
      "8.18052242173 9.95737\n",
      "9.4081897806 9.92991\n",
      "9.33938999337 9.90039\n",
      "10.3886159164 9.9248\n",
      "10.9320446741 9.97516\n",
      "10.225204838 9.98766\n",
      "10.3089972527 10.0037\n",
      "9.31510652559 9.9693\n",
      "9.58031921303 9.94985\n",
      "9.74438420514 9.93958\n"
     ]
    }
   ],
   "source": [
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for i in range(len(raw_data)):\n",
    "        summary_str, curr_avg = sess.run([merged, update_avg], feed_dict={curr_value: raw_data[i]})\n",
    "        sess.run(tf.assign(prev_avg, curr_avg))\n",
    "        print(raw_data[i], curr_avg)\n",
    "        writer.add_summary(summary_str, i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Check out the visualization by running TensorBoard from the terminal:\n",
    "\n",
    "    $ tensorboard --logdir=path/to/logs"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
